package at.ac.tuwien.infosys.aic11.dto;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name = "credit_request")
@XmlAccessorType(XmlAccessType.NONE)
public class CreditRequest {
	
	@XmlAttribute(name="request_id", required=true)
	private long requestId;
	
	@XmlElement(name="reason", required=true)
	private String reason;
	
	@XmlElement(name="offer", required=false)
	private Offer offer;
	
	@XmlElement(name="money", required=true)
	private Money money;
	
	@XmlElement(name="duration", required=true)
	private Duration duration;
	
	@XmlElementWrapper(name = "warrantors")
	@XmlElement(name = "warrantor", required=false)
	private List<Warrantor> warrantors = new ArrayList<Warrantor>();
	 
	@XmlElement(name="customer", required=true)
	private InternalCustomer customer;
	
	public CreditRequest(){}
	
	public CreditRequest(long requestId, String reason, Offer offer,
			Money money, Duration duration, InternalCustomer customer){
		this.requestId=requestId;
		this.reason=reason;
		this.offer=offer;
		this.money=money;
		this.duration=duration;
		this.customer=customer;
	}
	
	public long getRequestId() {
		return requestId;
	}

	public void setRequestId(long requestId) {
		this.requestId = requestId;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public Offer getOffer() {
		return offer;
	}

	public void setOffer(Offer offer) {
		this.offer = offer;
	}

	public Money getMoney() {
		return money;
	}

	public void setMoney(Money money) {
		this.money = money;
	}

	public Duration getDuration() {
		return duration;
	}

	public void setDuration(Duration duration) {
		this.duration = duration;
	}
	
	public List<Warrantor> getWarrentors() {
		return warrantors;
	}

	public void setWarrentors(List<Warrantor> warrentors) {
		this.warrantors = warrentors;
	}
	
	public void addWarrentor(Warrantor w){
		if(!warrantors.contains(w)){
			warrantors.add(w);
		}
	}
	
	public InternalCustomer getCustomer() {
		return customer;
	}

	public void setCustomer(InternalCustomer customer) {
		this.customer = customer;
	}

	public String toString() {
		return "id=" + requestId + " reason=" + reason + " customer=" + customer.getLastName() + " " + warrantors.size() + " warrantors" + " money=" + money.toString() + " duration=" + duration.getYears();
	}

}
